/**
 * 
 */
package com.opengroupe.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.transaction.annotation.Transactional;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.opengroupe.dto.LightResultatQuestionnaire;
import com.opengroupe.dto.SingleQuestionReponse;
import com.opengroupe.entities.Candidat;
import com.opengroupe.entities.Question;
import com.opengroupe.entities.Reponse;

/**
 * @author MMB11447
 *
 */
public class HeavyResultatQuestionnaire {
	
	private Candidat candidat;
	
	private Map<Question, List<Reponse>> questionReponses;
	
	private Map<Question, Boolean> resultat;
	
	private double pourcentage;

	/**
	 * Default class
	 */
	public HeavyResultatQuestionnaire() {
	}

	/**
	 * @param candidat
	 * @param questionReponsesCandidat
	 * @param resultat
	 */
	public HeavyResultatQuestionnaire(Candidat candidat,
			Map<Question, List<Reponse>> questionReponsesCandidat,
			Map<Question, Boolean> resultat) {
		super();
		this.candidat = candidat;
		this.questionReponses = questionReponsesCandidat;
		this.resultat = resultat;
		buildPourcentage();
	}

	/**
	 * @return the candidat
	 */
	public Candidat getCandidat() {
		return candidat;
	}

	/**
	 * @param candidat the candidat to set
	 */
	public void setCandidat(Candidat candidat) {
		this.candidat = candidat;
	}

	/**
	 * @return the questionReponses
	 */
	public Map<Question, List<Reponse>> getQuestionReponses() {
		return questionReponses;
	}

	/**
	 * @param questionReponses the questionReponses to set
	 */
	public void setQuestionReponses(Map<Question, List<Reponse>> questionReponses) {
		this.questionReponses = questionReponses;
	}

	/**
	 * @return the resultat
	 */
	public Map<Question, Boolean> getResultat() {
		return resultat;
	}

	/**
	 * @param resultat the resultat to set
	 */
	public void setResultat(Map<Question, Boolean> resultat) {
		this.resultat = resultat;
	}

	/**
	 * @return the percentage
	 */
	public double getPourcentage() {
		return pourcentage;
	}

	/**
	 * @param pourcentage the percentage to set
	 */
	public void setPourcentage(double pourcentage) {
		this.pourcentage = pourcentage;
	}
	
	/**
	 * Construit un objet et retourne un LightResultatQuestionnaire sous forme String JSON
	 * @return
	 */
	@Transactional
	public String getLightResultatQuestionnaire() {
		
		LightResultatQuestionnaire lightResultatQuestionnaire = new LightResultatQuestionnaire();
		lightResultatQuestionnaire.setCandidat(QrhUtil.convertCandidatToCandidatDto(candidat));
		lightResultatQuestionnaire.setPourcentage(pourcentage);
		
		List<SingleQuestionReponse> resultatQuestionnaire = new ArrayList<SingleQuestionReponse>();
		Set<Question> questions = questionReponses.keySet();
		for(Question question: questions) {
			SingleQuestionReponse sqr = new SingleQuestionReponse();
			
			sqr.setContenuQuestion(question.getContenuQuestion());
			
			List<Reponse> reponses = questionReponses.get(question);
			List<String> reponsesCandidat = new ArrayList<String>();
			for(Reponse reponse: reponses) {
				reponsesCandidat.add(reponse.getContenuReponse());
			}
			sqr.setReponsesCandidat(reponsesCandidat);
			
			List<Reponse> reponsesCorrectesList = new ArrayList<Reponse>(question.getReponsesCorrectes());
			List<String> reponsesCorrectes = new ArrayList<String>();
			for(Reponse reponse : reponsesCorrectesList){
				reponsesCorrectes.add(reponse.getContenuReponse());
			}
			sqr.setReponsesCorrectes(reponsesCorrectes);
			sqr.setResultat(Boolean.toString(resultat.get(question).booleanValue()));
			
			resultatQuestionnaire.add(sqr);
		}
		lightResultatQuestionnaire.setResultatQuestionnaire(resultatQuestionnaire);
		
		Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
		return gson.toJson(lightResultatQuestionnaire, LightResultatQuestionnaire.class);
	}


	/**
	 * Calculates the percentage of correct answers
	 */
	private void buildPourcentage() {
		int count = 0;
		Collection<Boolean> goods = resultat.values();
		for(Boolean bool: goods) {
			if(bool) {
				count++;
			}
		}

		double percentage = 100.0 * count / goods.size();
		setPourcentage(percentage);
	}

}
